<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript" src="Jquery/jquery-1.8.3.min.js"></script>

<?php
function get_region_group($province,$city,$area,$prefix=''){
	$doc=new DOMDocument();
	if(!$doc->load('region-cn-min.xml')) exit('地区XML文件不存在');
		
	$nodeP=null;
	$nodeC=null;
	$selected='';
	$v='';
	// 省份
	$return='<select name="'.$prefix.'Province">';
	$provinces=$doc->getElementsByTagName('province');
	foreach($provinces as $a){
		$v=$a->getAttribute('name');
		$selected='';
		if($province && $province==$v){
			$selected=' selected="selected"';
			$nodeP=$a;
		}
		$return.='<option value="'.$v.'"'.$selected.'>'.$v.'</option>';
	}
	$return.='</select>';
	// 城市
	if($nodeP==null) $nodeP=$provinces[0];
	$return.='<select name="'.$prefix.'City">';
	foreach($nodeP->childNodes as $a){
		$v=$a->getAttribute('name');
		$selected='';
		if($city && $city==$v){
			$selected=' selected="selected"';
			$nodeC=$a;
		}
		$return.='<option value="'.$v.'"'.$selected.'>'.$v.'</option>';
	}
	$return.='</select>';
	// 地区
	if($nodeC==null) $nodeC=$nodeP->childNodes[0];
	$return.='<select name="'.$prefix.'City">';
	foreach($nodeC->childNodes as $a){
		$v=$a->getAttribute('name');
		$selected='';
		if($area && $area==$v){
			$selected=' selected="selected"';
		}
		$return.='<option value="'.$v.'"'.$selected.'>'.$v.'</option>';
	}
	$return.='</select>';
	return $return;
}
?>
</head>

<body>
<select name="Province"></select><select name="City"></select><select name="Area"></select>
<script type="text/javascript">
var urlregion='../Api/region.php';
$('select[name="Province"]').load(urlregion,{field:'province',value:'福建省'},function(){ 
	var $province=$(this);
	$province.change(function(){
		province_change($province);
	});
	$province.next().load(urlregion,{field:'city',value:'厦门市',province:$province.val()},function(){
		var $city=$(this);
		$city.change(function(){	
			city_change($city);
		});
		$city.next().load(urlregion,{field:'area',value:'思明区',province:$province.val(),city:$city.val()});
	});
});
// 基础函数
function province_change($province){
		$city=$province.next();
		$area=$province.next().next();
		$city.load(urlregion,{field:'city',province:$province.val()},function(){
			$area.load(urlregion,{field:'area',province:$province.val(),city:$city.val()});	
		});
}
function city_change($city){
		$area=$city.next();
		$province=$city.prev();
		$area.load(urlregion,{field:'area',province:$province.val(),city:$city.val()});
}
</script>
</body>
</html>